package com.freemalll.auth.cloud.business.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freemalll.auth.cloud.business.domain.dto.SysApplicationDto;
import com.freemalll.auth.cloud.business.domain.entity.SysApplication;
import com.freemalll.auth.cloud.business.domain.vo.SysApplicationVo;
import com.freemalll.auth.cloud.business.mapper.SysApplicationMapper;
import com.freemalll.auth.cloud.business.service.ISysApplicationService;
import com.freemalll.common.core.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 系统应用 服务层实现
 *
 * @author freemalll
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysApplicationServiceImpl extends ServiceImpl<SysApplicationMapper, SysApplication> implements ISysApplicationService {
    @Override
    public Page<SysApplicationVo> queryPage(SysApplicationDto dto) {
        return baseMapper.queryPage(dto.page(), dto);
    }

    @Override
    public List<SysApplicationVo> queryList(SysApplicationDto dto) {
        return baseMapper.queryList(dto);
    }

    @Override
    public void insertApplication(SysApplicationDto dto) {
        SysApplication sysApplication = baseMapper.selectOne(Wrappers.<SysApplication>lambdaQuery().eq(SysApplication::getAppCode, dto.getAppCode()));
        if (sysApplication != null) {
            throw BusinessException.businessValidateError("新增应用失败，应用编码已存在");
        }
        SysApplication convert = Convert.convert(SysApplication.class, dto);
        baseMapper.insert(convert);
    }

    @Override
    public SysApplicationVo getInfoByAppId(String appId) {
        SysApplication sysApplication = baseMapper.selectById(appId);
        if (sysApplication == null) {
            throw BusinessException.businessDataNotExist("应用不存在");
        }
        return Convert.convert(SysApplicationVo.class, sysApplication);
    }

    @Override
    public void updateApplication(SysApplicationDto dto) {
        SysApplication application = baseMapper.selectById(dto.getAppId());
        if (application == null) {
            throw BusinessException.businessDataNotExist("应用不存在");
        }
        SysApplication sysApplication = baseMapper.selectOne(Wrappers.<SysApplication>lambdaQuery()
                .eq(SysApplication::getAppCode, dto.getAppCode())
                .notIn(SysApplication::getAppId, dto.getAppId()));
        if (sysApplication != null) {
            throw BusinessException.businessValidateError("更新应用失败，应用编码已存在");
        }
        BeanUtils.copyProperties(dto, application);
        baseMapper.updateById(application);
    }

    @Override
    public void deleteApplication(String appId) {
        if (!StringUtils.hasText(appId)) {
            throw BusinessException.businessValidateError("删除应用失败，appId不能为空");
        }
        baseMapper.deleteById(appId);
    }
}
